package jp.co.apl.haven.ao;

import java.sql.Connection;

import jp.co.apl.haven.ao.entity.AnswerEntity;
import jp.co.apl.haven.ao.entity.CategoryEntity;
import jp.co.apl.haven.ao.entity.EnqueteEntity;
import jp.co.apl.haven.ao.entity.QuestionEntity;
import jp.co.apl.haven.ao.entity.SelectionItemEntity;
import jp.co.apl.haven.generated.HavenProtos.InputType;
import junit.framework.TestCase;
import net.java.ao.DBParam;
import net.java.ao.EntityManager;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class HavenAoModuleCheck extends TestCase {

    private Injector injector;

    protected void setUp() throws Exception {
        super.setUp();

        injector = Guice.createInjector(new HavenAoModule());
    }

    public void testGo() throws Exception {

        EntityManager em = injector.getInstance(EntityManager.class);
        assertNotNull(em);

        Connection conn = em.getProvider().getConnection();
        conn.createStatement().execute("delete from answer");
        conn.createStatement().execute("delete from selection_item");
        conn.createStatement().execute("delete from question");
        conn.createStatement().execute("delete from category");
        conn.createStatement().execute("delete from enquete");

        {
            EnqueteEntity e =
                em.create(EnqueteEntity.class, new DBParam("version", 1));
            e.save();

            CategoryEntity c =
                em.create(CategoryEntity.class, new DBParam("name", "カテゴリ"));
            c.setEnquete(e);
            c.save();

            QuestionEntity q =
                em.create(
                    QuestionEntity.class,
                    new DBParam("title", "質問１"),
                    new DBParam("text", "質問１の答えは？"),
                    new DBParam("questionId", 1));
            q.setCategory(c);
            q.setInputType(InputType.SELECT);
            q.save();

            SelectionItemEntity s1 =
                em.create(SelectionItemEntity.class, new DBParam(
                    "selectionItemId",
                    1), new DBParam("value", "はい"));
            s1.setQuestion(q);
            s1.save();
            SelectionItemEntity s2 =
                em.create(SelectionItemEntity.class, new DBParam(
                    "selectionItemId",
                    2), new DBParam("value", "いいえ"));
            s2.setQuestion(q);
            s2.save();
        }

        {
            EnqueteEntity[] e = em.find(EnqueteEntity.class, "version=?", 1);
            assertEquals(1, e.length);
            assertEquals(
                "質問１",
                e[0].getCategories()[0].getQuestions()[0].getTitle());
            assertEquals(
                "質問１の答えは？",
                e[0].getCategories()[0].getQuestions()[0].getText());
        }

        {

            em.create(
                AnswerEntity.class,
                new DBParam("terminalId", "ABC"),
                new DBParam("questionId", 1),
                new DBParam("answer", "はい"));
        }

    }
}
